package com.yycx.module.system.provider.service.impl;

import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.system.client.entity.SysBlacklist;
import com.yycx.module.system.provider.mapper.SysBlacklistMapper;
import com.yycx.module.system.provider.service.SysBlacklistService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 黑名单 实现类
 *
 * @author flyme
 * @date 2019-07-04
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysBlacklistServiceImpl extends BaseServiceImpl<SysBlacklistMapper, SysBlacklist> implements SysBlacklistService {



    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public ResultBody beforeListEntityMap(CriteriaQuery<SysBlacklist> cq, SysBlacklist sysBlacklist, EntityMap requestMap) {
        Long userId = OpenHelper.getUserId();
        cq.select(SysBlacklist.class, "blacklistId", "targetId");
        cq.eq(SysBlacklist.class, "ownerId", userId);
        cq.like(SysBlacklist.class, "targetName");
        return super.beforeListEntityMap(cq, sysBlacklist, requestMap);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<SysBlacklist> cq, SysBlacklist sysBlacklist, EntityMap requestMap) {
        Long userId = OpenHelper.getUserId();
        cq.select(SysBlacklist.class, "blacklistId", "targetId", "targetName", "createTime");
        cq.eq(SysBlacklist.class, "ownerId", userId);
        cq.like(SysBlacklist.class, "targetName");
        return super.beforePageList(cq, sysBlacklist, requestMap);
    }


    @Override
    public ResultBody beforeAdd(CriteriaSave cs, SysBlacklist blacklist,EntityMap extra) {
        Long userId = OpenHelper.getUserId();
        blacklist.setOwnerId( userId);
        return ResultBody.ok();
    }

    @Override
    public SysBlacklist findByTargetName(String targetName) {
        CriteriaQuery cq=new CriteriaQuery(SysBlacklist.class);
        cq.eq(true,"targetName",targetName);
        cq.limit(1);
        return getOne(cq);
    }
}
